#include "sleep_widget.h"
#include "ui_sleep_widget.h"
#include <QKeyEvent>

Sleep_Widget::Sleep_Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Sleep_Widget)
{
    ui->setupUi(this);
    connect(&sleep_timer,SIGNAL(timeout()),this,SLOT(on_sleep_timer()));
    sleep_timer.start(1000);
    setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint|Qt::X11BypassWindowManagerHint|Qt::Tool);
    showFullScreen();
}

Sleep_Widget::~Sleep_Widget()
{
    delete ui;
}

void Sleep_Widget::on_sleep_timer(){
    if(sleep_time<=0){
        sleep_timer.stop();
        emit about_close();
        close();
        return;
    }
    QString str=[](int t){
        return QString("剩余休息时间：%1").arg(t/60,2,10,QLatin1Char('0'))+":"+QString("%1").arg(t%60,2,10,QLatin1Char('0'));
    }(--sleep_time<0?0:sleep_time);
    ui->msg_lab->setText(str);
    setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint|Qt::X11BypassWindowManagerHint|Qt::Tool);
    showFullScreen();
    this->activateWindow();
}

void Sleep_Widget::keyPressEvent(QKeyEvent *event){
    if(event->key()==Qt::Key_Q&&event->modifiers()&Qt::ControlModifier){
        sleep_time=0;
    }
}
